// by Xeno

private ["_dir_to_set","_m_name","_marker","_d_medtent", "_exit_it","_m_text"];

if ((player getVariable "d_medtent_available")== 0) exitWith{localize "MSG_DOM_MASHCANNOTBUILD" call XfGlobalChat};
if (d_player_busy_building) exitWith{localize "MSG_DOM_ALREADYBUSY" call XfGlobalChat};
if ((player call XfGetHeight) > 5) exitWith {localize "MSG_DOM_YOUAREKIDDING" call XfGlobalChat};

_d_medtent = player getVariable "d_medtent";
if (count _d_medtent > 0) exitWith {localize "MSG_DOM_MASHALREADYPLACED" call XfGlobalChat};

_d_medtent = player modeltoworld [0,5,0];

if (surfaceIsWater [_d_medtent select 0, _d_medtent select 1]) exitWith {
	localize "MSG_DOM_CANNOTBUILDWATER" call XfGlobalChat;
};
d_player_busy_building = true;

_helper1 = "HeliHEmpty" createVehicleLocal [_d_medtent select 0, (_d_medtent select 1) + 4, 0];
_helper2 = "HeliHEmpty" createVehicleLocal [_d_medtent select 0, (_d_medtent select 1) - 4, 0];
_helper3 = "HeliHEmpty" createVehicleLocal [(_d_medtent select 0) + 4, _d_medtent select 1, 0];
_helper4 = "HeliHEmpty" createVehicleLocal [(_d_medtent select 0) - 4, _d_medtent select 1, 0];

_exit_it = false;
if ((abs (((getPosASL _helper1) select 2) - ((getPosASL _helper2) select 2)) > 2) || (abs (((getPosASL _helper3) select 2) - ((getPosASL _helper4) select 2)) > 2)) then {
	localize "MSG_DOM_PLACENOTVALID" call XfGlobalChat;
	_exit_it = true;
};

for "_mt" from 1 to 4 do {call compile format ["deleteVehicle _helper%1;", _mt]};

if (_exit_it) exitWith {d_player_busy_building = false;};


for "_ic" from 1 to mash_build_duration do {
	player playMove "AinvPknlMstpSlayWrflDnon_medic";
	sleep 3;
	waitUntil {animationState player != "AinvPknlMstpSlayWrflDnon_medic"};
	if (!(alive player)) exitWith {};
};
d_player_busy_building = false;
if (!(alive player)) exitWith {localize "MSG_DOM_DIEDBEFOREBUILD" call XfGlobalChat};

_dir_to_set = getdir player - 180;

player setVariable ["d_medtent", _d_medtent];

_medic_tent = createVehicle ["Mash", _d_medtent, [], 0, "NONE"];
_medic_tent setdir _dir_to_set;
[_medic_tent, 0] call XfSetHeight;

player setVariable ["medic_tent", _medic_tent];
player reveal _medic_tent;

localize "MSG_DOM_MASHREADY" call XfGlobalChat;
_m_name = format [localize "MSG_DOM_MASHMRK", str(player)];
_m_text = format [localize "MSG_DOM_MASHMRK", name(player)]; 
["d_p_o_a", [str(player), [_medic_tent,_m_name,name player,playerSide,_m_text]]] call XNetCallEvent;

_medic_tent addAction [localize "MSG_DOM_MASHREMOVE" call XRedText, "client\removemash.sqf"];
_medic_tent addEventHandler ["killed",{[_this select 0] spawn XMashKilled}];
